# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=openturns
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.21
pkgrel=2
pkgdesc="Uncertainty treatment library (mingw-w64)"
license=('spdx:GPL-3.0-or-later')
arch=(any)
mingw_arch=('mingw64' 'mingw32' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="http://www.openturns.org/"
depends=("${MINGW_PACKAGE_PREFIX}-ceres-solver"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-mpc"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-muparser"
         "${MINGW_PACKAGE_PREFIX}-nlopt"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-dill"
         "${MINGW_PACKAGE_PREFIX}-python-psutil")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-swig")
source=("https://github.com/openturns/openturns/archive/v${pkgver}.tar.gz"
        "001-fix-default-site-packages.patch")
sha256sums=('f91f9b37c738c99761b3fd9eb2d50b2c1e3a8e4466f0f842fdc2445128cb5bda'
            'd62063cb3ac4857b3882ead29727df8414e47de0b8be7d915db5b1a7a6aea339')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  # https://github.com/openturns/openturns/pull/2005
  patch -p1 -i "${srcdir}"/001-fix-default-site-packages.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  local -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DOPENTURNS_DOC_PATH=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DOPENTURNS_DOC_PATH=${MINGW_PREFIX}/share/licenses/${_realname} \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DUSE_SPHINX=OFF \
    ${extra_config[@]} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="$pkgdir" ${MINGW_PREFIX}/bin/cmake --install .

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*

  local PREFIX_MINGW=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_MINGW}|\${PACKAGE_PREFIX_DIR}|g" \
    -i "$pkgdir${MINGW_PREFIX}/lib/cmake/${_realname}/OpenTURNSConfig.cmake"
  find "$pkgdir${MINGW_PREFIX}/lib/cmake/${_realname}" -name '*.cmake' -exec \
    sed -s "s|${PREFIX_MINGW}|\$\{_IMPORT_PREFIX\}|g" -i {} \;

  local PREFIX_MSYS2=$(cygpath -am /)
  sed -s "s|${PREFIX_MSYS2::-1}||g" \
    -i "$pkgdir${MINGW_PREFIX}/lib/cmake/${_realname}/OpenTURNSConfig.cmake"
}
